/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { NullPointerException, IllegalArgumentException } from './errors';

/**
 * @private
 *
 * @param assertion
 * @param msg
 * @param error
 */
export function assert(assertion, msg, error) {
    if(!assertion){
        if (error) {
            throw new error(msg);
        } else {
            throw new Error(msg);
        }
    }
}

/**
 * @private
 *
 * @param value
 * @param parameterName
 * @returns {*}
 */
export function requireNonNull(value, parameterName) {
    if (value == null) {
        throw new NullPointerException(`${parameterName} must not be null`);
    }
    return value;
}

/**
 * @private
 *
 * @param value
 * @param _class
 * @param parameterName
 * @returns {_class}
 */
export function requireInstance(value, _class, parameterName) {
    if (!(value instanceof _class)) {
        throw new IllegalArgumentException(`${parameterName} must be an instance of ${_class.name ? _class.name : _class}${value && value.constructor && value.constructor.name ? `, but is ${value.constructor.name}` : ''}`);
    }
    return value;
}

/**
 * @private
 *
 * @param methodName
 */
export function abstractMethodFail(methodName){
    throw new TypeError(`abstract method "${methodName}" is not implemented`);
}
